/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function(){
    var DialogLine={
        "intervalId":null,
        "scrollToBottom":function(){
            return $(".dialogue-con .dialogue-content-box").scrollTop($("#message-dialog-box")[0].scrollHeight);
        },
        "getChatMessages":function(){
            return e=$("#message-dialog-box").find("li:last").attr("data-timestamp"),$.ajax({
                url:chat_show_message_path({since_timestamp:e,polling:true}),type:"GET",
                async:true,dataType:"json",timeout:10000,success:function(data){
                    if(data.str){
                        $("#message-dialog-box").append(data.str);
                        DialogLine.scrollToBottom();
                    }
                }
            });
        },
        "ChatMessages":function(obj){
           var message=$("#dialog-messge").val(),action=$("#dialog_action").val(),no_vilad=$(obj).attr("data-disvilad-val"),vilad=$(obj).html();
           if(message == ""){
               layer.msg("内容不能为空哦",{icon:0,time:1000,offset:0});return false;
           }
           $(obj).html(no_vilad).addClass("nvilad");
           return $.ajax({
                url:chat_send_message_path({}),type:"POST",data:{message:message,act:"send",action:action,send_timestamp:Math.round(new Date()/1000)},
                async:true,dataType:"json",timeout:10000,success:function(data){
                    $(obj).html(vilad).removeClass("nvilad");
                    if(data.reqStatus){
                        $("#dialog-messge").val("").focus();
                        $("#message-dialog-box").append(data.data.mess);
                        DialogLine.scrollToBottom();
                    }else{
                        layer.msg("内容不能为空哦",{icon:0,time:1000,offset:0});return false;
                    }
                    
                }
            });
            return false;
        },
        "MoreMessages":function(obj){
            return _self=$(obj),_url=_self.attr("data-action-url"),$.ajax({
                url:_url,type:"GET",async:true,dataType:"json",timeout:10000,success:function(data){
                    if(data.reqStatus){
                        if(data.data.flag==0){
                            _self.css("display","none");
                        }
                        _self.attr("data-action-url",data.data.url);
                        $("#message-dialog-box").prepend(data.data.str);
                    }else{
                        layer.msg("内容不能为空哦",{icon:0,time:1000,offset:0});return false;
                    }

                }
            });
        }
    }
    $(window).focus(function() {
        return DialogLine.intervalId ? void 0 : DialogLine.intervalId = setInterval(DialogLine.getChatMessages, 2000);
    });
    $(window).blur(function(){
        return DialogLine.intervalId ? (clearInterval(DialogLine.intervalId), DialogLine.intervalId=0) : void 0;
    });
    $(window).mouseover(function() {
        return DialogLine.intervalId ? void 0 : DialogLine.intervalId = setInterval(DialogLine.getChatMessages, 2000);
    });
//    $(window).mouseout(function() {
//        return DialogLine.intervalId ? (clearInterval(DialogLine.intervalId), DialogLine.intervalId=0) : void 0;
//    });
    $("#send-message-btn").click(function(){
        return DialogLine.ChatMessages(this);
    });
    $("body").keydown(function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13 ){ 
            $("#send-message-btn").click();
         }
    });
    $("#dialogue-more").click(function(){
        DialogLine.MoreMessages(this);
    });
    $("#dialog-messge").val("").focus();
    DialogLine.scrollToBottom();
});
function chat_show_message_path(obj){
    var c_url = window.location.href,c_url_arr=c_url.split(".html");
    return c_url_arr[0]+"/chat_message.json?since_timestamp="+obj.since_timestamp+"&polling="+obj.polling;
}
function chat_send_message_path(obj){
    var c_url = window.location.href,c_url_arr=c_url.split(".html");
    return c_url_arr[0]+"/send_message";
}